package com.duapp.main;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 * 显示微博所带的图片
 * 
 * @author 蜗牛都知道
 * @see <a href="http://binary.duapp.com/"></a>
 */
public class Loading extends JDialog {
	private static final long serialVersionUID = 2763736129158509416L;
	private BufferedImage bi = null;

	public Loading(String filename) {
		setLayout(new BorderLayout());
		setUndecorated(true);
		try {
			bi = javax.imageio.ImageIO.read(new File(filename));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		int imgHeight = bi.getHeight();
		int imgWidth = bi.getWidth();
		JPanel imgPanel = new JPanel() {
			private static final long serialVersionUID = 6246862165441423926L;

			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2d = (Graphics2D) g;
				if (bi != null) {
					g2d.drawImage(bi, 0, 0, this);
				}
			}
		};
		imgPanel.setOpaque(false);
		imgPanel.setSize(imgWidth, imgHeight);
		add(imgPanel);
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					dispose();
				}
			}
		});
		imgPanel.setToolTipText("按Esc键退出！");
		setSize(imgWidth, imgHeight);
		setLocationRelativeTo(getOwner());
		setVisible(true);
	}

	public static void main(String[] args) {
		new Loading("images/loading.png");
	}
}